package com.ruoyi.common.utils.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.inter.HttpHostProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 通用http发送方法
 *
 * @author ruoyi
 */
@Component
public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
    private static HttpHostProvider httpHostProvider;

    @Autowired
    public void setHttpHostProvider(HttpHostProvider provider) {
        httpHostProvider = provider;
    }

    public static String httpStr(String url, String method, JSONObject body, JSONObject header, String contentType) {
        String res = "";
        try {
            JSONObject http = new JSONObject();
            http.put("url", url);
            http.put("method", method);
            if (header != null) {
                http.put("header", header);
            }
            if (body != null) {
                http.put("body", body);
            }
            if (contentType != null) {
                http.put("contentType", contentType);
            }
            res = HttpUtil.createPost(httpHostProvider.getHttpHostOfJd() + "cp/httpStr")
                    .header("Content-Type", "application/json")
                    .body(http.toJSONString())
                    .timeout(10000)
                    .execute().body();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return res;
    }

    public static String httpStr(String url, String method, JSONObject body, JSONObject header) {
        return httpStr(url, method, body, header, null);
    }

    public static JSONObject httpJson(String url, String method, JSONObject body, JSONObject header) {
        return httpJson(httpHostProvider.getHttpHostOfJd(), url, method, body, header, 15000);
    }

    public static JSONObject httpJsonNew(String reqUrl, String url, String method, JSONObject body,
                                         JSONObject header, JSONObject... others) {
        return httpJsonNew(httpHostProvider.getHttpHostOfJd(), reqUrl, url, method, body, 15000, header, others);
    }

    public static JSONObject httpJsonNewHwy(String reqUrl, String url, String method, JSONObject body,
                                            JSONObject header, JSONObject... others) {
        return httpJsonNew(httpHostProvider.getHttpHostOfHwy(), reqUrl, url, method, body, 15000, header, others);
    }

    public static JSONObject httpJsonNewHg(String reqUrl, String url, String method, JSONObject body,
                                           JSONObject header, JSONObject... others) {
        return httpJsonNew(httpHostProvider.getHttpHostOfHg(), reqUrl, url, method, body, 15000, header, others);
    }

    public static JSONObject httpJsonNewZyt(String reqUrl, String url, String method, JSONObject body,
                                            JSONObject header, JSONObject... others) {
        return httpJsonNew(httpHostProvider.getHttpHostOfZyt(), reqUrl, url, method, body, 15000, header, others);
    }

    public static JSONArray httpArray(String url, String method, JSONObject body, JSONObject header) {
        return httpArray(httpHostProvider.getHttpHostOfHwy(), url, method, body, header, 15000);
    }

    public static JSONObject httpJsonOfJd(String url, String method, JSONObject body, JSONObject header, Integer timeout) {
        return httpJson(httpHostProvider.getHttpHostOfJd(), url, method, body, header, timeout);
    }

    public static JSONObject httpJsonOfJd(String url, String method, JSONObject body, JSONObject header) {
        return httpJson(httpHostProvider.getHttpHostOfJd(), url, method, body, header, 15000);
    }

    public static JSONObject httpJsonOfHwy(String url, String method, JSONObject body, JSONObject header) {
        return httpJson(httpHostProvider.getHttpHostOfHwy(), url, method, body, header, 15000);
    }

    public static JSONObject httpJsonOfZyt(String url, String method, JSONObject body, JSONObject header) {
        return httpJson(httpHostProvider.getHttpHostOfZyt(), url, method, body, header, 15000);
    }

    public static JSONObject httpJsonOfHwyAo5(String url, String method, JSONObject body, JSONObject header) {
        return httpAo5(httpHostProvider.getHttpHostOfHwy(), url, method, body, header, 15000);
    }

    public static JSONObject httpAo5(String host, String url, String method, JSONObject body, JSONObject header, Integer timeout) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject http = new JSONObject();
            http.put("url", url);
            http.put("method", method);
            if (header != null) {
                http.put("header", header);
            }
            if (body != null) {
                http.put("body", body);
            }
            HttpRequest request = HttpUtil.createPost(host + "cp/httpAo5")
                    .header("Content-Type", "application/json")
                    .body(http.toJSONString());

            if (timeout != null) {
                request.timeout(timeout);
            }
            String res = request.execute().body();
            jsonObject = JSONObject.parseObject(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jsonObject;
    }

    public static JSONObject httpJson(String host, String url, String method, JSONObject body, JSONObject header, Integer timeout) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject http = new JSONObject();
            http.put("url", url);
            http.put("method", method);
            if (header != null) {
                http.put("header", header);
            }
            if (body != null) {
                http.put("body", body);
            }
            HttpRequest request = HttpUtil.createPost(host + "cp/httpJSON")
                    .header("Content-Type", "application/json")
                    .body(http.toJSONString());

            if (timeout != null) {
                request.timeout(timeout);
            }
            String res = request.execute().body();
            jsonObject = JSONObject.parseObject(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jsonObject;
    }

    public static JSONArray httpArray(String host, String url, String method, JSONObject body, JSONObject header, Integer timeout) {
        JSONArray array = new JSONArray();
        try {
            JSONObject http = new JSONObject();
            http.put("url", url);
            http.put("method", method);
            if (header != null) {
                http.put("header", header);
            }
            if (body != null) {
                http.put("body", body);
            }
            HttpRequest request = HttpUtil.createPost(host + "cp/httpArray")
                    .header("Content-Type", "application/json")
                    .body(http.toJSONString());

            if (timeout != null) {
                request.timeout(timeout);
            }
            String res = request.execute().body();
            array = JSONArray.parseArray(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return array;
    }

    private static JSONObject httpJsonNew(String host, String reqUrl, String url, String method, JSONObject body,
                                          Integer timeout, JSONObject header, JSONObject... others) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject http = new JSONObject();
            http.put("url", url);
            http.put("method", method);
            if (header != null) {
                http.put("header", header);
            }
            if (body != null) {
                http.put("body", body);
            }
            if (others != null) {
                for (JSONObject obj : others) {
                    http.putAll(obj);
                }
            }
            HttpRequest request = HttpUtil.createPost(host + reqUrl)
                    .header("Content-Type", "application/json")
                    .body(http.toJSONString());

            if (timeout != null) {
                request.timeout(timeout);
            }
            String res = request.execute().body();
            jsonObject = JSONObject.parseObject(res);
        } catch (Exception e) {
            log.error(url);
            log.error(e.getMessage(), e);
        }
        return jsonObject;
    }
}